package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "地址簿接口")
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询所有地址信息
     * @return
     */
    @ApiOperation("查询所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> list(){
        log.info("查询所有地址信息...");
        List<AddressBook> addressBookList= addressBookService.list();
        return Result.success(addressBookList);
    }

    /**
     * 查询默认地址
     * @return
     */
    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> select(){
        log.info("查询默认地址...");
        AddressBook addressBook=addressBookService.select();
        return Result.success(addressBook);
    }

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @ApiOperation("添加地址")
    @PostMapping
    public Result insert(@RequestBody AddressBook addressBook){
        log.info("添加地址信息：{}",addressBook);
        addressBookService.insert(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("把地址为：{}的地址设置成默认",addressBook);
        addressBookService.setDefault(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> selectById(Long id){
        log.info("要查询的地址id=={}",id);
        AddressBook addressBook= addressBookService.selectById(id);
        return Result.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @ApiOperation("修改地址")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址信息：{}",addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @ApiOperation("删除地址")
    @DeleteMapping
    public Result delete(Long id){
        log.info("要删除的地址id=={}",id);
        addressBookService.delete(id);
        return Result.success();
    }

}
